#include "genomeprojectuploader.h"

GenomeProjectUploader::GenomeProjectUploader(QString f, QObject *parent) :
    QObject(parent), projectFile(f)
{
//    //--------INSTANTIATE THE DATABASE---------
//    //  TO DO:  shouldn't be constant data type- need to get type from file.
//    if( DATA_TYPE == PROPRIETARY_DATA )
//    {
//        databaseInterface = new ProprietaryGenomeLocalDatabaseInterface(this);
//    }
//    else if( DATA_TYPE == PUBMED_DATA )
//    {
//        databaseInterface = new PubMedGenomeLocalDatabaseInterface(this);
//    }
}

void GenomeProjectUploader::readData()
{
    int contigCount = 0;
    //now read gff2 file and output the reformated file

    QFile inFile(projectFile);

    if( !inFile.exists() )
    {
        qDebug() << "file not found." << endl;
        return ;
    }

    if( ! inFile.open(QIODevice::ReadOnly))
    {
        qDebug() << "file " << projectFile << " can't be opened." << endl;
        return ;
    }

    qDebug() << "file " << projectFile << " opened." << endl;

    //this is for csv files... works for now
    QTextStream inStream(&inFile);
    QString line = "";

    //Get first line and throw it away- it's the project file
    line = inStream.readLine();

    QPointer<ContigDataPacket> c;
    do {
        line = inStream.readLine();
        //divide line into major tokens- by the tab delim
       QStringList majorTokens = line.split("\t", QString::SkipEmptyParts);

       //read contig
        if( majorTokens.size() > 0 && majorTokens[0].contains("Contig:") )
        {
            QString contigId = majorTokens[1];
            qDebug() << "contigId = " << contigId << endl;
            int length = majorTokens[3].toInt();//TO DO:  use this somehow?
            bool hasSpecies = (majorTokens[5].contains("T")?true:false);
            QString species = majorTokens[7];
            c = new ContigDataPacket(contigId, hasSpecies, species);

            contigCount++;

            //get next line and throw it away
            line = inStream.readLine();

            bool hasMoreGenes = true;
            QString geneLine = "";
            while( hasMoreGenes && !inStream.atEnd() ) //instream should never be reached first
            {
                geneLine = inStream.readLine();
                QStringList geneTokens = geneLine.split("\t");//, QString::SkipEmptyParts);
                if( geneTokens[0].contains("end genes") )
                {
                    hasMoreGenes = false;
                }
                else //is a gene
                {

                    QString seqId = geneTokens[0];
                    QString source = geneTokens[1];
                    QString type = geneTokens[2];
                    int start = geneTokens[3].toInt();
                    int stop = geneTokens[4].toInt();
                    QString ps = geneTokens[5];
                    bool posStrand = (geneTokens[6].contains("T")?true:false);
                    QString parentContigId = geneTokens[6];
                    bool hasCluster = (geneTokens[7].contains("T")?true:false);
                    QString clusterId = geneTokens[8];
                    bool hasSpecies = (geneTokens[9].contains("T")?true:false);
                    QString species = geneTokens[10];

                    if( DATA_TYPE == PUBMED_DATA )
                    {
                        QString id = geneTokens[11];
                        QString name = geneTokens[12];
                        QString product = geneTokens[13];
                        QString parentElement = geneTokens[14];
//                        QString seqId, QString source, QString type, int start, int stop, bool posStrand,
//                                                                    QString parentContigId, bool hasCluster, QString clusterId,
//                                                                    bool hasSpecies, QString species,
//                                                                    QString id, QString name, QString product, QString parentElement,
//                                                                    QObject *parent = 0);
                        PubMedGenomeElementDataPacket* p = new PubMedGenomeElementDataPacket( seqId, source, type, start, stop, posStrand,
                                                                                              parentContigId, hasCluster, clusterId,
                                                                                              hasSpecies, species,
                                                                                              id, name, product, parentElement );
                        if( contigCount < 1000 )
                        {
                            genomeElementDataPackets << p;
                            c->addGenomeElement(p);
                            //databaseInterface->addGenomeElement(p); //add element to database
                        }
                        else
                        {
                           // databaseInterface->addGenomeElementThenDeleteIt( p ) ;
                        }
                    }
                    else
                    {
                        //TO DO
                    }

                }
            }
            contigDataPackets << c ;
            //done reading genes
        }
        //done reading contig
    } while (!inStream.atEnd() );


    qDebug() << genomeElementDataPackets.size() << " " << contigDataPackets.size();

    emit( dataUploadCompleteSignal(genomeElementDataPackets, contigDataPackets, contigCount) );
}
